package com.archstoday.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.archstoday.R;
import com.archstoday.entity.TopicDetailComentEntity;
import com.archstoday.utils.ToolUtils;
import com.archstoday.view.MyListView;
import com.bumptech.glide.Glide;

import java.util.List;

import de.hdodenhof.circleimageview.CircleImageView;

public class CommentForTopicAdapter extends BaseAdapter {

    private Context mContext;
    private List<TopicDetailComentEntity.DataModelXX.CommentsModel.DataModel> mData;
    private onReplyListener mListener;

    public CommentForTopicAdapter(Context context) {
        mContext = context;
    }

    public void setListener(onReplyListener listener) {
        mListener = listener;
    }

    public void setData(List<TopicDetailComentEntity.DataModelXX.CommentsModel.DataModel> data) {
        mData = data;
        notifyDataSetChanged();
    }

    public void addData(List<TopicDetailComentEntity.DataModelXX.CommentsModel.DataModel> data) {
        mData.addAll(data);
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        if (mData == null) {
            return 0;
        }
        return mData.size();
    }

    @Override
    public TopicDetailComentEntity.DataModelXX.CommentsModel.DataModel getItem(int i) {
        return mData.get(i);
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        Holder holder;
        if (view == null) {
            holder = new Holder();
            view = LayoutInflater.from(mContext).inflate(R.layout.item_comment_for_topic, null);
            holder.userHead = (CircleImageView) view.findViewById(R.id.item_user_head);
            holder.userName = (TextView) view.findViewById(R.id.item_user_name_tv);

            holder.userTime = (TextView) view.findViewById(R.id.item_user_time_tv);
            holder.userComment = (TextView) view.findViewById(R.id.item_user_comment_tv);
            holder.recyclerView = (MyListView) view.findViewById(R.id.id_for_user_reply);

            view.setTag(holder);
        } else {
            holder = (Holder) view.getTag();
        }

        TopicDetailComentEntity.DataModelXX.CommentsModel.DataModel.CommentModel comment = mData.get(i).getComment();

        holder.userName.setText(comment.getUserName());
        holder.userComment.setText(comment.getCommentText());
        Glide.with(mContext).load(comment.getUserImg()).error(R.mipmap.user_header_icon).into(holder.userHead);

        holder.userTime.setText(ToolUtils.getCreatTime
                (String.valueOf(comment.getCreateTime())));

        if (mData.get(i).getReplies() != null && mData.get(i).getReplies().size() > 0) {
            holder.recyclerView.setVisibility(View.VISIBLE);

            CommentForTopicReplyAdapter commentForTopicReplyAdapter = new CommentForTopicReplyAdapter(mContext, mData.get(i).getReplies());
            holder.recyclerView.setAdapter(commentForTopicReplyAdapter);

            holder.recyclerView.setOnItemClickListener(new onItemClick(mData.get(i), commentForTopicReplyAdapter));
        } else {
            holder.recyclerView.setVisibility(View.GONE);
        }

        return view;
    }

    class Holder {
        MyListView recyclerView;
        CircleImageView userHead;
        TextView userName, userTime, userComment;
    }

    class onItemClick implements AdapterView.OnItemClickListener {

        private CommentForTopicReplyAdapter mAdapter;
        TopicDetailComentEntity.DataModelXX.CommentsModel.DataModel comment;

        public onItemClick(TopicDetailComentEntity.DataModelXX.CommentsModel.DataModel c, CommentForTopicReplyAdapter adapter) {
            mAdapter = adapter;
            comment = c;
        }

        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
            mListener.reply(comment, mAdapter.getItem(i));
        }
    }

    public interface onReplyListener {
        void reply(TopicDetailComentEntity.DataModelXX.CommentsModel.DataModel data,TopicDetailComentEntity.DataModelXX.CommentsModel.DataModel.RepliesModel info);
    }

}